;$(function () {
    // layui.config({base: '{/}../static/layui_ext/dist/'});
    layui.extend({
        selectY: '{/}../static/layui_ext/selectY/selectY'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['form', 'selectY'], function () {
        var form = layui.form,
            selectY = layui.selectY;
        // $.ajax({
        //     type: 'POST',
        //     url: '../store/listArea',
        //     dataType: 'JSON',
        //     async: false,
        //     error: function (data) {
        //         layer.msg('稍后再试！');
        //     },
        //     success: function (data) {
        //         if (data.code === true) {
        //             selectY({
        //                 elem: '#demo',
        //                 data: data.data,
        //                 placeholder: '请选择地址',//初始提示
        //                 disabledTips: '',//被禁用时提示
        //                 success: function (e) { //回调
        //                     console.log(e.data);//选择的name值
        //                     console.log(e.ids);//选择的id数据
        //                 }
        //             });
        //
        //         } else {
        //             layer.msg(data.msg);
        //         }
        //     },
        // });

        $("#storeInfoUpdate").click(function () {
            let locationIds;
            layer.open({
                type: 1,
                title: "修改超市信息",
                skin: 'layui-layer-rim',//加边框
                area: ['500px', '480px'],
                content: $("#storeEdit"),//注意放在<body>最外层
                btn: ['确认', '取消'],
                success: function (index, layero) {
                    $("#storeEdit").removeClass("layui-hide");
                    $('#storeInfoForm')[0].reset();
                    let storeId = $("#storeId").val();
                    let storeName = $("#storeName").val();
                    let location = $("#storeLocation").val();
                    let locationId = $("#storeLocation").attr("data-id");
                    let detailedAddress = $("#storeDetailedAddress").val();
                    let contacts = $("#storeContacts").val();
                    let phone = $("#storePhone").val();
                    form.val("storeInfoForm", {
                        "id": storeId
                        , "storeName": storeName
                        , "location": location
                        , "detailedAddress": detailedAddress
                        , "contacts": contacts
                        , "phone": phone
                    });
                    $("#location").attr('data-id', locationId);
                    form.render();
                    $.ajax({
                        type: 'POST',
                        url: 'listArea',
                        dataType: 'JSON',
                        async: false,
                        error: function (data) {
                            layer.msg('稍后再试！');
                        },
                        success: function (data) {
                            if (data.code === true) {
                                selectY({
                                    elem: '#location',
                                    data: data.data,
                                    placeholder: '请选择地址',//初始提示
                                    disabledTips: '',//被禁用时提示
                                    success: function (e) { //只有选择了才会触发的回调
                                        // console.log(e.data);//选择的name值
                                        // console.log(e.ids);//选择的id数据
                                        locationIds = e.ids
                                    }
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                    });
                },
                yes: function (index, layero) {
                    form.on('submit(storeInfoForm)', function (data) {
                        if (isEmpty(locationIds)) {
                            locationIds = $("#location").attr("data-id");
                        }
                        $("#storeInfoForm").ajaxSubmit({
                            type: 'POST',
                            async: false,
                            url: "updateStore",
                            dataType: 'json',
                            data: {"locationId": locationIds.toString()},
                            success: function (data) {
                                if (data.code === true) {
                                    layer.msg('修改成功！');
                                    layer.close(index);
                                    setTimeout("location.reload()", 1000);
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                layer.msg('请稍后再试！');
                            },
                        });
                        return false;
                    });
                    $("#storeInfoForm").submit();
                },
                btn2: function (index, layero) {
                    layer.close(index);
                },
                end: function () {
                    $("#storeEdit").addClass("layui-hide");
                    //解决关闭窗口后再打开会出现重复元素的问题
                    let clone = $("#location").clone();
                    clone.removeAttr("data-id");
                    clone.removeAttr("value");
                    $("#storeEdit").find('.selectY-box').before(clone);
                    $("#storeEdit").find('.selectY-box').remove();
                }
            });


        });
        $("#storeInsert").click(function () {
            let locationIds;
            layer.open({
                type: 1,
                title: "新建超市",
                skin: 'layui-layer-rim',//加边框
                area: ['500px', '480px'],
                content: $("#storeAdd"),//注意放在<body>最外层
                btn: ['确认', '取消'],
                success: function (index, layero) {
                    $("#storeAdd").removeClass("layui-hide");
                    $('#storeAddInfoForm')[0].reset();
                    form.val("storeAddInfoForm", {
                        "storeName": ""
                        , "location": ""
                        , "detailedAddress": ""
                        , "contacts": ""
                        , "phone": ""
                    });
                    form.render();
                    $.ajax({
                        type: 'POST',
                        url: '../store/listArea',
                        dataType: 'JSON',
                        async: false,
                        error: function (data) {
                            layer.msg('稍后再试！');
                        },
                        success: function (data) {
                            if (data.code === true) {
                                selectY({
                                    elem: '#locationAdd',
                                    data: data.data,
                                    placeholder: '请选择地址',//初始提示
                                    disabledTips: '',//被禁用时提示
                                    success: function (e) { //只有选择了才会触发的回调
                                        // console.log(e.data);//选择的name值
                                        // console.log(e.ids);//选择的id数据
                                        locationIds = e.ids
                                    }
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        },
                    });
                },
                yes: function (index, layero) {
                    form.on('submit(storeAddInfoForm)', function (data) {
                        if (isEmpty(locationIds)) {
                            // locationIds = $("#location").attr("data-id");
                            layer.msg("请选择地址");
                            return false;
                        }
                        $("#storeAddInfoForm").ajaxSubmit({
                            type: 'POST',
                            async: false,
                            url: "../store/insertStore",
                            dataType: 'json',
                            data: {"locationId": locationIds.toString()},
                            success: function (data) {
                                if (data.code === true) {
                                    layer.msg('创建成功！');
                                    layer.close(index);
                                    setTimeout("location.reload()", 1000);
                                } else {
                                    layer.msg(data.msg);
                                }
                            },
                            error: function (data) {
                                layer.msg('请稍后再试！');
                            },
                        });
                        return false;
                    });
                    $("#storeAddInfoForm").submit();
                },
                btn2: function (index, layero) {
                    layer.close(index);
                },
                end: function () {
                    $("#storeAdd").addClass("layui-hide");
                    //解决关闭窗口后再打开会出现重复元素的问题
                    let clone = $("#locationAdd").clone();
                    clone.removeAttr("data-id");
                    clone.removeAttr("value");
                    $("#storeAdd").find('.selectY-box').before(clone);
                    $("#storeAdd").find('.selectY-box').remove();
                }
            });


        })
    });
});